/* Turska UI library
 * Copyright 2003-2008 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: UIButton.cpp,v 1.2 2008/03/27 11:44:26 tonic Exp $
 * $Revision: 1.2 $
 */

#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "SDL.h"
#include "SDL_opengl.h"
#include "UI.h"


UIButton::UIButton(INT x, INT y, UIImage *defaultImage, UIImage *pressedImage,
                   UIImage *symbolImage, UIFont *font, const char *text,
                   UINT32 fontFlags, INT textX, INT textY,
                   INT itemPressedXOffset, INT itemPressedYOffset) :
    UIComponent(x, y, 0, 0), mIsPressed(false),
    mDefaultImage(defaultImage), mPressedImage(pressedImage),
    mSymbolImage(symbolImage), mSymbolImageDrawMode(UIImage::NORMAL),
    mSymbolImageFlags(UIImage::COLOR | UIImage::TRANSFORM | UIImage::CENTER),
    mFont(font), mText(NULL), mFontFlags(fontFlags), mTextX(textX), mTextY(textY),
    mItemPressedXOffset(itemPressedXOffset), mItemPressedYOffset(itemPressedYOffset)
{
    UINT a;
    for (a = 0; a < 4; ++a)
        mColor[a] = mItemColor[a] = 1;

    UIImage *sizeImage = NULL;

    if (text != NULL)
    {
        setText(text);
        assert(mText != NULL);
    }

    if (mDefaultImage != NULL)
        sizeImage = mDefaultImage;
    else if (mPressedImage != NULL)
        sizeImage = mPressedImage;
    else if (mSymbolImage != NULL)
        sizeImage = mSymbolImage;
 
    if (sizeImage != NULL)
    {
        setWidth(sizeImage->getWidth());
        setHeight(sizeImage->getHeight());
    }
    else if (mText != NULL && mFont != NULL)
    {
        setWidth(mFont->getStringWidth(mText));
        setHeight(mFont->getFontHeight());
    }

    if (mTextX == -1)
        mTextX = getWidth() / 2;
    if (mTextY == -1)
        mTextY = getHeight() / 2;
}


UIButton::~UIButton()
{
    if (mText)
        free(mText);
    mText = NULL;
}


void UIButton::setText(const char *text)
{
    if (mText)
        free(mText);
    if (text)
        mText = strdup(text);
    else
        mText = NULL;
}


void UIButton::setColor(FLOAT red, FLOAT green, FLOAT blue, FLOAT alpha)
{
    mColor[0] = red;
    mColor[1] = green;
    mColor[2] = blue;
    mColor[3] = alpha;
}


void UIButton::setItemColor(FLOAT red, FLOAT green, FLOAT blue, FLOAT alpha)
{
    mItemColor[0] = red;
    mItemColor[1] = green;
    mItemColor[2] = blue;
    mItemColor[3] = alpha;
}


void UIButton::onPointerDown(INT x, INT y)
{
    UIComponent::onPointerDown(x, y);
    mIsPressed = isInside(x, y);
}


void UIButton::onPointerUp(INT x, INT y)
{
    UIComponent::onPointerUp(x, y);
    if (isPressed() && isInside(x, y))
    {
        EventListenerSet::iterator it = mEventListeners.begin();
        EventListenerSet::iterator itend = mEventListeners.end();
        for (; it != itend; ++it)
            (*it)->onAction(this);
    }
    mIsPressed = false;
}


void UIButton::onPointerMove(INT x, INT y)
{
    UIComponent::onPointerMove(x, y);
    mIsPressed = isPointerDown() && isInside(x, y);
}


void UIButton::draw()
{
    UIImage *image = mDefaultImage;

    if (isPressed() && mPressedImage != NULL)
        image = mPressedImage;

    if (image != NULL)
        image->draw(mImageDrawMode, mImageFlags, mX, mY, mColor);

    INT itemXOffset = isPressed() ? mItemPressedXOffset : 0;
    INT itemYOffset = isPressed() ? mItemPressedYOffset : 0;

    if (mSymbolImage != NULL)
        mSymbolImage->draw(mSymbolImageDrawMode, mSymbolImageFlags, mX + mWidth / 2, mY + mHeight / 2, mItemColor);
    if (mText != NULL)
        mFont->drawString(mX + mTextX + itemXOffset, mY + mTextY + itemYOffset, mFontFlags, mText, mItemColor);
}
